<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>登记出库</span>
          </div>
          <div class="save_button">
            <div v-if="reviewAdmin" class="buttonBox">
              <el-button type="success" icon="el-icon-check" size="mini" @click="reviewAdminHandle">确认审核</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" size="mini" icon="el-icon-document-checked" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" size="mini" icon="el-icon-document-remove" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-form ref="form" :model="data" :inline="true" :rules="formRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row :gutter="10">
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="coding">
                <div>编号</div>
                <el-input v-model="data.coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="name">
                <div>名称</div>
                <el-input v-model="data.name" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="status">
                <div>状态</div>
                <el-select v-model="data.status" size="mini">
                  <el-option v-for="(item, index) in appearStatus" :key="index" :value="index" :label="item" :disabled="true" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="apply_admin">
                <div>领用人</div>
                <el-input v-model="data.apply_admin" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="apply_class">
                <div>领用班组</div>
                <el-input v-model="data.apply_class" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="workshop_id">
                <div>领用车间</div>
                <el-select v-model="data.workshop_id" size="mini">
                  <el-option v-for="(item, index) in toolAppearWorkshop" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="store_id">
                <div>仓库</div>
                <v-select v-model="data.store_id" action="storeList" size="mini" :query="{kind:1}" disabled />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="appear_date">
                <div>出库时间</div>
                <el-date-picker v-model="data.appear_date" type="datetime" size="mini" placeholder="选择日期" align="right" value-format="yyyy-MM-dd HH:mm:ss" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="admin_id">
                <div>工具库管</div>
                <v-select v-model="data.admin_id" size="mini" :query="{jobs:'116,117,118'}" />
              </el-form-item>
            </el-col>
            <div v-if="data.verify_admin">
              <el-col :xl="3" :lg="4" :md="5" :sm="6">
                <el-form-item>
                  <div>验证人员</div>
                  <el-input v-model="data.verify_admin.name" :disabled="true" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="5" :sm="6">
                <el-form-item>
                  <div>验证时间</div>
                  <el-date-picker v-model="data.verify_date" type="date" size="mini" :disabled="true" />
                </el-form-item>
              </el-col>
            </div>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="kind">
                <div>类型</div>
                <el-select v-model="data.kind" size="mini">
                  <el-option v-for="(item, index) in toolAppearKind" :key="index" :value="index" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="sort">
                <div>排序</div>
                <el-input v-model="data.sort" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs>
          <el-tab-pane label="详细">
            <el-table :data="list">
              <el-table-column label="工具" prop="tools_true.name" min-width="16%" />
              <el-table-column label="型号" prop="tools_true.spec" min-width="9%" />
              <el-table-column label="单位" prop="tools_true.unit" min-width="8%" />
              <el-table-column label="单价" prop="tools_true.price" min-width="8%" />
              <el-table-column label="供应商" prop="providers.name" min-width="13%" />
              <el-table-column label="仓库" prop="tools_true.stores.name" min-width="8%" />
              <el-table-column label="是否在帐" min-width="8%">
                <template v-slot="{row}">
                  {{ invisible[row.invisible] }}
                </template>
              </el-table-column>
              <el-table-column label="数量" prop="number" min-width="9%" />
              <el-table-column label="备注" prop="notice" min-width="13%" />
              <el-table-column label="操作" prop="" min-width="8%">
                <template scope="scope">
                  <div v-if="canAppend">
                    <el-button type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
                  </div>
                  <div v-else>
                    -
                  </div>
                </template>
              </el-table-column>
              <div v-if="canAppend" slot="append">
                <el-form ref="tool_form" :model="tool" :inline="true" class="hf_form append_other_admin" :rules="toolRules">
                  <el-row :gutter="10">
                    <el-col :span="4">
                      <el-form-item prop="tool_id">
                        <v-select-s v-model="tool.tool_id" action="toolList" :query="{store_id:data.store_id,add_field:'spec'}" :filterable="true" :remote="true" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item>
                        <el-input v-model="tool.spec" disabled size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item>
                        <el-input v-model="tool.unit" disabled size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item>
                        <el-input v-model="tool.price" disabled size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="3">
                      <el-form-item prop="provider_id">
                        <v-select v-model="tool.provider_id" :disabled="!tool.tool_id" :query="{part_id:tool.tool_id,kind: 2}" action="providerList" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item>
                        <el-input v-model="tool.stores" disabled size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item prop="invisible">
                        <el-select v-model="tool.invisible" size="mini">
                          <el-option v-for="(item, index) in invisible" :key="index" :value="index" :label="item" />
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item prop="number">
                        <el-input v-model="tool.number" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="3">
                      <el-form-item prop="notice">
                        <el-input v-model="tool.notice" size="mini" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="2">
                      <el-form-item>
                        <el-button icon="el-icon-upload2" size="mini" @click="append" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="描述">
            <el-row :gutter="10">
              <el-col :span="4">
                <div>
                  <span>创建时间日期</span>
                  <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" :disabled="true" size="mini" />
                </div>
                <div>
                  <span>创建人</span>
                  <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" :disabled="true" size="mini" />
                </div>
                <div v-if="data.update_time">
                  <div>
                    <span>最近更新</span>
                    <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" :disabled="true" size="mini" />
                  </div>
                  <div>
                    <span>更新者</span>
                    <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" :disabled="true" size="mini" />
                  </div>
                </div>
              </el-col>
              <el-col :span="20">
                <Tinymce id="content" v-model="data.content" />
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/tool/appear'
import { appearStatus, toolAppearWorkshop, toolAppearKind, invisible } from '@/utils/options'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
import Tinymce from '@/components/Tinymce/index'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendParts', 'removeParts', 'goUrl'],
  components: {
    VSelect,
    VSelectS,
    Tinymce
  },
  data() {
    return {
      data: {
        'update_time': '',
        'cr_admin': {
          'name': ''
        },
        'verify_admin': {
          'name': ''
        },
        'class_a': {
          'class_n': {
            'name': ''
          }
        },
        'up_admin': ''
      },
      reviewAdmin: false,
      list: [],
      tool: {},
      appearStatus,
      toolAppearWorkshop,
      toolAppearKind,
      invisible,
      canAppend: false,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入出库单名称',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        apply_admin: [
          {
            required: true,
            message: '请填写领用人',
            trigger: 'blur'
          }
        ],
        apply_class: [
          {
            required: true,
            message: '请填写领用班组',
            trigger: 'blur'
          }
        ],
        workshop_id: [
          {
            required: true,
            message: '请选择申请车间',
            trigger: 'change'
          }
        ],
        store_id: [
          {
            required: true,
            message: '请选择仓库',
            trigger: 'change'
          }
        ],
        appear_date: [
          {
            required: true,
            message: '请输入出库时间',
            trigger: 'blur'
          }
        ],
        admin_id: [
          {
            required: true,
            message: '请选择负责人',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      toolRules: {
        tool_id: [
          {
            required: true,
            message: '请选择工具',
            trigger: 'change'
          }
        ],
        provider_id: [
          {
            required: true,
            message: '请选择供应商',
            trigger: 'change'
          }
        ],
        invisible: [
          {
            required: true,
            message: '请选择是否在帐',
            trigger: 'change'
          }
        ],
        number: [
          {
            required: true,
            message: '请输入数量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.error_code === 87404) {
        this.$alert('您请求的工具出库单不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      console.log(response.data)
      this.data = response.data
      this.list = response.data.tool_list
      if (response.data.status === 0) {
        this.reviewAdmin = true
        this.canAppend = true
      }
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.tool_form
      const appear_data = {
        'tool_id': this.tool.tool_id,
        'invisible': this.tool.invisible,
        'number': this.tool.number,
        'notice': this.tool.notice,
        'provider_id': this.tool.provider_id,
        'appear_id': this.$route.params.id
      }
      const errorCode = 87004
      const errorMessage = '该工具已存在'
      this.appendParts(API, thisForm, appear_data, errorCode, errorMessage)
    },
    remove(id) {
      const tool_data = {
        'id': id
      }
      this.removeParts(API, id, tool_data)
    },
    reviewAdminHandle() {
      const reviewData = {
        'status': 1,
        'content': this.data.content,
        'id': this.data.id
      }
      API.review(reviewData).then(response => {
        if (response.data.error_code === 1) {
          this.$message.success('确认！')
          this.backUrl()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    goBack() {
      this.goUrl('/tool/appear')
    }
  }
}
</script>
